package com.taptech.doufu.ui.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.taptech.doufu.constant.HomePageType;
import com.taptech.doufu.listener.IListScollerState;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.view.AutoPollAdapter;
import com.taptech.doufu.ui.view.AutoPollRecyclerView;
import com.taptech.doufu.ui.view.viewflow.view.CircleFlowIndicator;
import com.taptech.doufu.ui.view.viewflow.view.HomeViewFlow;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.util.DensityUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.CpLibListActivity;
import com.taptech.doufu.ui.adapter.DfhomeNovelAdapter3;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.DFHomeCategoryBean;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.MarketNovelNoticeBean;
import com.taptech.doufu.bean.base.BaseResponseBean;
import com.taptech.doufu.bean.novel.NovelSubBean;
import com.taptech.doufu.bean.novel.NovelSubGsonBean;
import com.taptech.doufu.bean.cp.NovelCPBanner;
import com.taptech.doufu.util.CommonUtils;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.constant.FragmentLoadType;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.ToUpOrDownable;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.presenter.home.HomeCategoryPresenter;
import com.taptech.doufu.presenter.home.HomeSpeakerPresenter;
import com.taptech.doufu.presenter.home.HomeTopicPresenter;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.StartActivityUtils;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.util.GsonUtil;
import com.taptech.doufu.ui.view.HomeBannerImageAdapter;
import com.taptech.doufu.ui.view.NetworkImageView;
import com.taptech.doufu.ui.view.PullToRefreshListView;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * 小说同人页面
 */
public class DFGAYFragment extends HomeBaseFragment implements
        PullToRefreshListView.OnLoadAndRefreshListener,
        HomeCategoryPresenter.OnHomeCategoryView,
        HomeSpeakerPresenter.OnHomeSpeakerView,
        HomeTopicPresenter.OnHomeTopicView{

    private static final String TAG = DFGAYFragment.class.getSimpleName();
    private static final int COUNT = 20;
    private PullToRefreshListView listView;
    private HomeViewFlow viewFlow;
    private HomeBannerImageAdapter viewFlowAdapter;
    private CircleFlowIndicator flowIndicator;
    private RelativeLayout line1, line2;
    private LinearLayout line_bg, llBroadcast, llAutoLayout;
    private NetworkImageView iv1, iv2;
    private TextView tv1, tv2;
    private List<DFHomeBannerBean> bannerList;
    private List<DFHomeCategoryBean> categoryList;
    private List<Object> contentAdaList;
    private TTHomeViewPager viewPager;
    private DfhomeNovelAdapter3 listAda;
    private String last = "";
    private AutoPollRecyclerView autoPollRecyclerView;
    private List<MarketNovelNoticeBean> listNotice;
    private ImageView ivTop;
    private boolean isShowGuessLove = true;
    private View rootView;
    private boolean isUseCache = true;
    private HomeCategoryPresenter categoryPresenter;
    private HomeSpeakerPresenter speakerPresenter;
    private HomeTopicPresenter topicPresenter;

    private IListScollerState iListScollerState;
    private PullToRefreshListView.OnScrollYListener onScrollYListener;

    public void setOnScrollYListener(PullToRefreshListView.OnScrollYListener onScrollYListener) {
        this.onScrollYListener = onScrollYListener;
    }

    public void setiListScollerState(IListScollerState iListScollerState) {
        this.iListScollerState = iListScollerState;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_dfcoterie_gay, null);
            getDefaultDisplay();
            initPresenter();
            initsView(rootView);
        }
        return rootView;
    }

    private void initPresenter() {
        topicPresenter = new HomeTopicPresenter(getActivity(),HomePageType.SAME_PERSON,this);
        speakerPresenter = new HomeSpeakerPresenter(getActivity(),this);
        categoryPresenter = new HomeCategoryPresenter(getActivity(),HomePageType.SAME_PERSON,this);
    }

    /**
     * 先显示缓存数据
     */
    private void loadCache() {
        loadBannerCache();
        if (categoryPresenter != null) {
            categoryPresenter.loadCategoryCache(ApiClient.SEX_TYPE_NO);
        }
        if (speakerPresenter != null) {
            speakerPresenter.loadSpeakerCache(ApiClient.SEX_TYPE_NO);
        }
        if (topicPresenter != null) {
            topicPresenter.loadTopicCache(ApiClient.SEX_TYPE_NO);
        }
    }


    private void getNovelSub() {
        if (AccountService.getInstance().isLogin()) {//登录
            getNovelSubData("");
        } else {//未登录
            listView.setFull(true);
            listView.onRefreshComplete();
            listView.loadMoreComplete();
            listView.setLoadmoreable(false);
        }
    }

    /**
     * 获取订阅数据
     * @param last
     */
    private void getNovelSubData(final String last) {
        ApiClient.getInstance().setUseCache(isUseCache).getService().getNovelSub(last,18,COUNT)
                .map(new Func1<NovelSubGsonBean, NovelSubGsonBean>() {
                    @Override
                    public NovelSubGsonBean call(NovelSubGsonBean novelSubGsonBean) {
                        if (TextUtils.isEmpty(last)) {
                            CacheUtil.saveJsonDataToCache(new Gson().toJson(novelSubGsonBean.getData()),new File(CommmonImp.Home_novel_gay_list2));
                        }
                        return novelSubGsonBean;
                    }
                })
                .compose(RxJavaHelper.<NovelSubGsonBean>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<NovelSubGsonBean>(){
                    @Override
                    public void onNext(NovelSubGsonBean novelSubGsonBean) {
                        super.onNext(novelSubGsonBean);
                        updateSubUi(novelSubGsonBean.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (listView != null) {
                            listView.onRefreshComplete();
                        }
                    }
                });
    }


    private void updateSubUi(NovelSubBean bean) {
        if (listView != null) {
            listView.onRefreshComplete();
        }
        List<DFHomeNovelBeans> tempAdapList = bean.getList();
        if ((tempAdapList != null) && (tempAdapList.size() > 0)) {
            addNewBean(tempAdapList);
        }
        if (!TextUtils.isEmpty(bean.getLast())) {
            String templast = bean.getLast();
            if (last.equals("") && tempAdapList.size() == 0) {
                listView.setFull(true);
                listView.loadMoreComplete();
                listView.setLoadmoreable(false);
            }
            if ((templast != null) && (templast.length() > 0) && (!last.equals(templast))) {
                listView.setLoadmoreable(true);
                last = templast;
            } else {
                listView.setFull(true);
                listView.loadMoreComplete();
                listView.setLoadmoreable(false);
            }
        }
        listAda.notifyDataSetChanged();
    }

    private void initsView(View rootView) {
        listView = (PullToRefreshListView) rootView.findViewById(R.id.fragment_dfnovel_list);
        listView.setRefreshable(true);
        listView.setOnLoadAndRefreshListener(this);

        listView.setMoveListener(new ToUpOrDownable() {
            @Override
            public void moveToUp() {
                ivTop.setVisibility(View.INVISIBLE);
                setToTopVisible(false);
            }

            @Override
            public void moveToDown(int firstVisiblePos) {
                if (firstVisiblePos > 3) {
                    ivTop.setVisibility(View.VISIBLE);
                    setToTopVisible(true);
                }
            }
        });
        listView.setOnScrollStateListent(new PullToRefreshListView.IOnScrollState() {
            @Override
            public void onState(int state) {
                if (iListScollerState != null) {
                    iListScollerState.scrollState(state);
                }
            }
        });
        ivTop = (ImageView) rootView.findViewById(R.id.fragment_dfnovel_upTop);
        ivTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToTop();
            }
        });
        initListViewHeadView();
        contentAdaList = new ArrayList<>();
        listAda = new DfhomeNovelAdapter3(getActivity(), contentAdaList, ApiClient.SEX_TYPE_NO);
        listView.setAdapter(listAda);
        listView.setOnScrollYListener(onScrollYListener);
    }

    public void scrollToTop() {
        if (listView != null) {
            listView.setSelection(0);
        }
        if (ivTop != null) {
            ivTop.setVisibility(View.INVISIBLE);

        }
    }

    public void stopSpeaker() {
        if (autoPollRecyclerView != null) {
            autoPollRecyclerView.stop();
        }
    }

    public void startSpeaker() {
        if (autoPollRecyclerView != null) {
            autoPollRecyclerView.start();
        }
    }

    private boolean toTopButtonIsShow = false;

    private void setToTopVisible(boolean visibleAnimation) {
        ObjectAnimator objectAnimators = null;
        AnimatorSet animatorSet = new AnimatorSet();
        if (visibleAnimation) {
            //出现动画
            if (!toTopButtonIsShow) {
                objectAnimators = ObjectAnimator.ofFloat(ivTop, "translationY", ScreenUtil.getScreenHeight(this.getActivity()), 0);
            }
            toTopButtonIsShow = true;
        } else {
            //隐藏动画
            if (toTopButtonIsShow) {
                objectAnimators = ObjectAnimator.ofFloat(ivTop, "translationY", 0, ScreenUtil.getScreenHeight(this.getActivity()) - ivTop.getMeasuredHeight());
            }
            toTopButtonIsShow = false;
        }
        if (objectAnimators != null) {
            animatorSet.setDuration(500);
            animatorSet.playTogether(objectAnimators);
            animatorSet.start();
        }

    }


    /**
     * request server
     */
    private void getDataFromNet() {
        isShowGuessLove = true;
        if (listView.getLoadCompleteImg() != null) {
            listView.getLoadCompleteImg().setVisibility(View.GONE);
        }
        listView.setFull(false);
        listView.setLoadmoreable(true);
        if (viewFlow != null) {
            viewFlow.stopAutoFlowTimer();
        }
        getHomeNovelGAYBanner();
        if (categoryPresenter != null) {
            categoryPresenter.getHomeNovelGAYCategory(isUseCache);
        }
        if (speakerPresenter != null) {
            speakerPresenter.getHomeNovelSpeaker("",isUseCache);
        }
        if (topicPresenter != null) {
            topicPresenter.getHomeGAYNovelTopic(isUseCache);
        }
    }

    /**
     * 装在headView,包括轮播图，下面的标签栏
     */
    private void initListViewHeadView() {
        View homeTopEnterView = LayoutInflater.from(getActivity()).inflate(R.layout.head_home_coterie_viewflow_gay, null);
        llBroadcast = (LinearLayout) homeTopEnterView.findViewById(R.id.ll_dfhome_viewflow_broatcast);
        llAutoLayout = (LinearLayout) homeTopEnterView.findViewById(R.id.fg_dfgay_notice_layout);
        viewFlow = (HomeViewFlow) homeTopEnterView.findViewById(R.id.dfhome_exchange_viewflow);
        viewFlow.setLayoutParams(new RelativeLayout.LayoutParams(ScreenUtil.getScreenWidth(getActivity()), (int) (ScreenUtil.getScreenWidth(getActivity())/1.5)));
        flowIndicator = (CircleFlowIndicator) homeTopEnterView.findViewById(R.id.dfhome_ad_indicator);
        flowIndicator.setVisibility(View.VISIBLE);
        autoPollRecyclerView = homeTopEnterView.findViewById(R.id.autoPollRecyclerView);
        line_bg = (LinearLayout) homeTopEnterView.findViewById(R.id.dfhome_novbel_bg);
        setBackgroudImage();

        line1 = (RelativeLayout) homeTopEnterView.findViewById(R.id.dfhome_novel_line1);
        line2 = (RelativeLayout) homeTopEnterView.findViewById(R.id.dfhome_novel_line2);
        setRelayout(line1);
        setRelayout(line2);

        tv1 = (TextView) homeTopEnterView.findViewById(R.id.dfhome_novel_tv1);
        tv2 = (TextView) homeTopEnterView.findViewById(R.id.dfhome_novel_tv2);

        setTextViewSize(tv1);
        setTextViewSize(tv2);

        iv1 = (NetworkImageView) homeTopEnterView.findViewById(R.id.dfhome_novel_iv1);
        iv2 = (NetworkImageView) homeTopEnterView.findViewById(R.id.dfhome_novel_iv2);

        setImageView(iv1);
        setImageView(iv2);

        listView.addHeaderView(homeTopEnterView);
    }

    @Override
    public synchronized void onRefresh() {
        if (listView.isRefreshable()) {
            listView.setLoadmoreable(false);
            last = "";
            lastRequest = "-99999999";
            //重新加载数据
            isUseCache = false;//下拉刷新时不使用缓存
            getDataFromNet();
        }
    }

    private String lastRequest = "-999999990";

    @Override
    public synchronized void loadMore() {
        if (listView.isLoadmoreable()) {
            isShowGuessLove = false;
            if (!CommonUtils.isNetWorkConnected(getActivity())) {
                listView.loadMoreComplete();
                listView.setLoadmoreable(true);
                return;
            }
            if (!lastRequest.equals(last)) {
                getNovelSubData(last);
                lastRequest = last;
            }
        }
    }

    private int getPx(int dimensionDp) {
        float density = getResources().getDisplayMetrics().density;
        return (int) (dimensionDp * density + 0.5f);
    }

    private synchronized void addNewBean(List<DFHomeNovelBeans> tempAdapList) {
        for (int i = 0; i < tempAdapList.size(); i++) {
            DFHomeNovelBeans cateBean = tempAdapList.get(i);
            if (0 == i) {
                if (isShowGuessLove) {
                    cateBean.hasTitleA = true;
                    cateBean.upTitleNameA = "我的订阅";
                    cateBean.isMoreUrlA = false;
                }
            }
            if (contentAdaList != null) {
                contentAdaList.add(cateBean);
            }
        }
    }

    private void initsDefaultCategoryData() {
        line1.setVisibility(View.INVISIBLE);
        line2.setVisibility(View.INVISIBLE);
    }

    private void initsCategoryData(List<DFHomeCategoryBean> tempCategoryList) {
        initsDefaultCategoryData();
        NetworkImageView ivTemp;
        TextView tvTemp;
        RelativeLayout lineTemp;
        for (int i = 0; (i < 2) && (i < tempCategoryList.size()); i++) {
            if (0 == i) {
                lineTemp = line1;
                ivTemp = iv1;
                tvTemp = tv1;
            } else if (1 == i) {
                lineTemp = line2;
                ivTemp = iv2;
                tvTemp = tv2;
            } else {
                lineTemp = null;
                ivTemp = null;
                tvTemp = null;
            }

            if (ivTemp != null) {
                ivTemp.setFullScreen(false);
                ivTemp.setRound(true);
                lineTemp.setVisibility(View.VISIBLE);
                setCategoryClickListener(lineTemp, tempCategoryList.get(i).getType(), i);
                ivTemp.setURL(tempCategoryList.get(i).getImgurl());
                tvTemp.setText(tempCategoryList.get(i).getTitle());
            }
        }
    }

    private void setCategoryClickListener(View clickView, final String viewType, final int pos) {
        clickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goWithCategoryClick(viewType, pos);
            }
        });
    }

    private void goWithCategoryClick(final String viewType, final int pos) {
        Intent intent;
        switch (viewType) {
            case "15"://cp库
                TMAnalysis.event(getActivity(), "MAIN_TR_CP");
                intent = new Intent(getActivity(), CpLibListActivity.class);
                startActivity(intent);
                break;
            case "16"://排行**
                TMAnalysis.event(getActivity(), "MAIN_TR_RANK");
                intent = new Intent();
                intent.putExtra(FragmentLoadType.fragmentTypeName, categoryList.get(pos).getTitle());
                intent.putExtra(Constant.URL, categoryList.get(pos).getMoreUrl() + "&");
                new StartActivityUtils(getActivity(), intent).toCpRankActivity();
                break;
            default:
                break;
        }
    }

    private boolean isAttachActivity = false;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        isAttachActivity = true;
    }

    private void initFlowData(List<DFHomeBannerBean> bannerList) {
        try {
            if (viewFlow != null && bannerList != null) {
                viewFlowAdapter = new HomeBannerImageAdapter(bannerList, getActivity(),HomePageType.SAME_PERSON);
                viewFlow.setAdapter(viewFlowAdapter);
                viewFlow.setmSideBuffer(bannerList.size());// 实际图片张数为size大小
                viewFlow.setTimeSpan(5000);// 图片切换时间间隔
                viewFlow.setSelection(3 * 1000); // 设置初始位置
                if (viewPager != null) {
                    viewFlow.setViewPage(viewPager);
                }
                flowIndicator.setViewFlow(viewFlow);
                viewFlow.setFlowIndicator(flowIndicator);
                if (bannerList.size() > 1) {
                    viewFlow.startAutoFlowTimer(); // 启动自动播放
                }
            } else {
                viewFlowAdapter.setData(bannerList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setViewPager(TTHomeViewPager mViewPager) {
        this.viewPager = mViewPager;
    }

    private int screenWidthPx;
    private int categoryItemNeddHeight;

    private void getDefaultDisplay() {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getActivity().getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidthPx = dpMetrics.widthPixels;
    }

    private void setRelayout(RelativeLayout rela) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rela.getLayoutParams();
        params.width = screenWidthPx;
        params.height = categoryItemNeddHeight;
        rela.setLayoutParams(params);
    }

    private void setImageView(NetworkImageView iv) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv.getLayoutParams();
        params.height = categoryItemNeddHeight * 34 / 61;
        params.width = params.height;
        params.leftMargin = 0;
        params.rightMargin = screenWidthPx * DensityUtil.dip2px(getActivity(), 8) / 720;
        iv.setLayoutParams(params);
    }

    private void setTextViewSize(TextView tv) {
        int textSize2 = DensityUtil.px2sp(getActivity(), (screenWidthPx * 15) / 360);
        tv.setTextSize(textSize2);
    }

    private void setBackgroudImage() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) line_bg.getLayoutParams();
        params.width = screenWidthPx;
        params.height = ((screenWidthPx * 122) / 360) / 2;
        categoryItemNeddHeight = params.height;
        line_bg.setLayoutParams(params);
    }


    /**
     * 切换主题
     */
    public void changeThemeWithColorful() {
        if (!isAttachActivity) {
            return;
        }
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            changeDarkViewBg();
        } else {
            changeDayViewBg();
        }
        if (listAda != null) {
            listAda = new DfhomeNovelAdapter3(getActivity(), contentAdaList, ApiClient.SEX_TYPE_NO);
            listView.setAdapter(listAda);
        }
        if (viewFlowAdapter != null) {
            initFlowData(bannerList);
        }
    }

    private void changeDayViewBg() {
        line_bg.setBackground(getResources().getDrawable(R.color.white));
        iv1.setAlpha(1.0f);
        iv1.setBackgroundColor(getResources().getColor(R.color.home_classify_img_bg));
        iv2.setAlpha(1.0f);
        iv2.setBackgroundColor(getResources().getColor(R.color.home_classify_img_bg));
        tv1.setTextColor(getResources().getColor(R.color.text_color_2));
        tv2.setTextColor(getResources().getColor(R.color.text_color_2));

        rootView.setBackgroundColor(getResources().getColor(R.color.all_activity_bg));
        llBroadcast.setBackgroundColor(getResources().getColor(R.color.white));
        llAutoLayout.setBackground(getResources().getDrawable(R.drawable.shape_roundrectangle));

    }

    private void changeDarkViewBg() {
        line_bg.setBackgroundColor(getResources().getColor(R.color.fg_dark));
        iv1.setAlpha(Constant.imagViewAlpha);
        iv1.setBackgroundColor(getResources().getColor(R.color.fg_dark));
        iv2.setAlpha(Constant.imagViewAlpha);
        iv2.setBackgroundColor(getResources().getColor(R.color.fg_dark));
        tv1.setTextColor(getResources().getColor(R.color.text_dark));
        tv2.setTextColor(getResources().getColor(R.color.text_dark));

        rootView.setBackground(getResources().getDrawable(R.color.bg_dark));
        llBroadcast.setBackground(getResources().getDrawable(R.color.fg_dark));
        llAutoLayout.setBackground(getResources().getDrawable(R.drawable.shape_roundrectangle_dark));


    }


    private void loadBannerCache() {
        Observable.create(new Observable.OnSubscribe<NovelCPBanner>() {
                    @Override
                    public void call(Subscriber<? super NovelCPBanner> subscriber) {
                        JSONObject json = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_novel_gay_banner));
                        NovelCPBanner data = GsonUtil.parseJsonWithGson(json.toString(), NovelCPBanner.class);
                        subscriber.onNext(data);
                    }
                })
                .compose(RxJavaHelper.<NovelCPBanner>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<NovelCPBanner>(){
                    @Override
                    public void onNext(NovelCPBanner novelCPBanner) {
                        super.onNext(novelCPBanner);
                        if (novelCPBanner != null) {
                            bannerList = novelCPBanner.getCarousels();
                            if (bannerList != null) {
                                initFlowData(bannerList);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    /**
     * 获取轮播图数据
     */
    private void getHomeNovelGAYBanner() {
        ApiClient.getInstance().setUseCache(isUseCache).getService().getNovelCPBanner()
                .map(new Func1<BaseResponseBean<NovelCPBanner>, BaseResponseBean<NovelCPBanner>>() {
                    @Override
                    public BaseResponseBean<NovelCPBanner> call(BaseResponseBean<NovelCPBanner> novelCPBannerBaseResponseBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(novelCPBannerBaseResponseBean.getData()),new File(CommmonImp.Home_novel_gay_banner));
                        return novelCPBannerBaseResponseBean;
                    }
                })
                .compose(RxJavaHelper.<BaseResponseBean<NovelCPBanner>>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<BaseResponseBean<NovelCPBanner>>() {
                    @Override
                    public void onNext(BaseResponseBean<NovelCPBanner> novelCPBanner) {
                        super.onNext(novelCPBanner);
                        bannerList = novelCPBanner.getData().getCarousels();
                        if (bannerList != null) {
                            initFlowData(bannerList);
                        }
                    }
                });
    }

    @Override
    public void fetchData() {
        loadCache();
        getDataFromNet();
    }


    @Override
    public void onSpeakerNext(List<MarketNovelNoticeBean> list, boolean isCache) {
        if (list != null) {
            listNotice = list;
            if (listNotice != null && listNotice.size() > 0) {
                if (!TextUtils.isEmpty(listNotice.get(0).getMsg())) {
                    llBroadcast.setVisibility(View.VISIBLE);
                    AutoPollAdapter adapter = new AutoPollAdapter(getActivity(), listNotice,HomePageType.SAME_PERSON);
                    autoPollRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                    autoPollRecyclerView.setAdapter(adapter);
                    autoPollRecyclerView.start();
                } else {
                    llBroadcast.setVisibility(View.GONE);
                }
            } else {
                llBroadcast.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onCategoryNext(List<DFHomeCategoryBean> list, boolean isCache) {
        if (list != null) {
            categoryList = list;
            initsCategoryData(categoryList);
        }
    }

    @Override
    public void onTopicNext(List<Object> list, boolean isCache) {
        contentAdaList = list;
        if (listAda != null&&isCache) {
            listAda.refrushList(contentAdaList);
        }else{
            listAda = new DfhomeNovelAdapter3(getActivity(), contentAdaList, ApiClient.SEX_TYPE_NO);
            listView.setAdapter(listAda);
        }
        if (!isCache) {
            getNovelSub();
        }
    }

    @Override
    public void onTopicError() {
        getNovelSub();
    }
}
